<?php
class Api_Model_Mixturas extends App_Model
{
    private $_name = 'Fundicion_Mixturas_Calculadas';

	public function __construct() {
		parent::__construct();
     }

	public function exportarMixturas($mixturas=NULL, $fetchMode=PDO::FETCH_OBJ) {
        // $this->_name especificas
        if (is_array($mixturas)) {
            $sql = 
                "SELECT 
                    DISTINCT M.*, E.*, M.MIXTURA AS MIXT, M.[FECHA CIERRE] AS CIERRE
                FROM $this->_name M
                LEFT JOIN Fundicion_Ensayes_Reales_Mixturas E ON 
                    E.[# Mixtura Tradicional] = M.ID
                WHERE M.ID IN (" . implode(',', $mixturas) .")";

            return $this->_conn->query($sql)->fetchAll($fetchMode);
        } else if (NULL !== $mixturas) {
            // Mixtura especifica
            $sql = <<<SQL
                SELECT 
                    DISTINCT M.*, E.*, M.MIXTURA AS MIXT, M.[FECHA CIERRE] AS CIERRE
                FROM $this->_name M
                LEFT JOIN Fundicion_Ensayes_Reales_Mixturas E ON 
                    E.[# Mixtura Tradicional] = M.ID
                WHERE M.ID = ?
SQL;

            $stmt = $this->_conn->prepare($sql);
            $stmt->execute(array($mixturas));

            if (FALSE === $stmt) {
                return $stmt;
            }

            return $stmt->fetchAll($fetchMode);
        } else {
            // Todas las mixturas :)
            $sql = <<<SQL
                SELECT 
                    DISTINCT M.*, E.*, M.MIXTURA AS MIXT, M.[fecha cierre] AS CIERRE
                FROM $this->_name M
                LEFT JOIN Fundicion_Ensayes_Reales_Mixturas E ON 
                    E.[# Mixtura Tradicional] = M.ID
SQL;

            return $this->_conn->query($sql)->fetchAll($fetchMode);
        }
	}

	public function getMixturas($fetchMode = PDO::FETCH_OBJ) {
		$sql = <<<SQL
			SELECT 
				DISTINCT M.*, E.[# Mixtura Tradicional] AS MIXTURA_CALCULADA 
			FROM 
				$this->_name M 
			LEFT JOIN 
				Fundicion_Ensayes_Reales_Mixturas E ON 
			E.[# Mixtura Tradicional] = M.ID
SQL;

		return $this->_conn->query($sql)->fetchAll($fetchMode);
	}

	public function addMixtura($mixtura) {
		// Denegar accion por edicion
        if (FALSE == EDITABLE) {
            return FALSE;
        }

        $sql = <<<SQL
	INSERT INTO $this->_name 
	(
        [fecha cierre],
        [Forecast],
        [% Sulfuros],
        [PESO HUMEDO],
        [% Oxidos (minerales)],
        [% Arena],
        [% Piedra de Cal],
        [% Secs Refineria],
        [% Secs Bermejillo],
        [% Secs de Zinc],
        [% Secs Fund],
        [% Casc Fe Tons],
        [% Ins],
        [% SiO2 CALC], 
        [% Fe CALC],
        [% CaO CALC],
        [% Zn CALC],
        [% S total CALC],
        [% S util CALC],
        [% Pb CALC],
        [% Cu CALC],
        [% As CALC],
        [% Sb CALC],
        [% Cd CALC],
        [gr/ton Au CALC],
        [kg/ton Ag CALC],
        [Ag/Pb CALC],
        [suma total ensayes CAL], 
        [Fe/SiO2 mixtura CALC],
        [Fe/SiO2 mixtura + cascarilla CALC],
        [CaO/SiO2 CALC],
        [Sb/Pb CALC],
        [Cu/Pb CALC],
        [Cu/As CALC],
        [As/Sb CALC],
        [Suma de ensayes funde],
        [Suma de ensayes no funde],
        [TOTAL SECS CALC],
        [Tonelaje Costo CALC], 
        [Suma de Fundentes], 
        [Cu/(As+Sb) Calc],
        [Fecha_Captura],
        [% S/SO4 CALC]
	)
	VALUES (
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?,
        ?
	)
SQL;

/**



**/

        
		// Insertar mixtura
        $query =
            $this->_conn->prepare($sql)
                ->execute(array(
                    $mixtura['fecha cierre'],
                    $mixtura['Forecast'],
                    $mixtura['% Sulfuros'],
                    $mixtura['PESO HUMEDO'],
                    $mixtura['% Oxidos (minerales)'],
                    $mixtura['% Arena'],
                    $mixtura['% Piedra de Cal'],
                    $mixtura['% Secs Refineria'],
                    $mixtura['% Secs Bermejillo'],
                    $mixtura['% Secs de Zinc'],
                    $mixtura['% Secs Fund'],
                    $mixtura['% Casc Fe Tons'],
                    $mixtura['% Ins'],
                    $mixtura['% SiO2 CALC'], 
                    $mixtura['% Fe CALC'],
                    $mixtura['% CaO CALC'],
                    $mixtura['% Zn CALC'],
                    $mixtura['% S total CALC'],
                    $mixtura['% S util CALC'],
                    $mixtura['% Pb CALC'],
                    $mixtura['% Cu CALC'],
                    $mixtura['% As CALC'],
                    $mixtura['% Sb CALC'],
                    $mixtura['% Cd CALC'],
                    $mixtura['gr/ton Au CALC'],
                    $mixtura['kg/ton Ag CALC'],
                    $mixtura['Ag/Pb CALC'],
                    $mixtura['suma total ensayes CAL'],
                    $mixtura['Fe/SiO2 mixtura CALC'],
                    $mixtura['Fe/SiO2 mixtura + cascarilla CALC'],
                    $mixtura['CaO/SiO2 CALC'],
                    $mixtura['Sb/Pb CALC'],
                    $mixtura['Cu/Pb CALC'],
                    $mixtura['Cu/As CALC'],
                    $mixtura['As/Sb CALC'],
                    $mixtura['Suma de ensayes funde'],
                    $mixtura['Suma de ensayes no funde'],
                    $mixtura['TOTAL SECS CALC'],
                    $mixtura['Tonelaje Costo CALC'], 
                    $mixtura['Suma de Fundentes'],
                    $mixtura['Cu/(As+Sb) Calc'],
                    $mixtura['Fecha_Captura'],
                    $mixtura['% S/SO4 CALC']
                ));
        
        if (FALSE === $query) {
            return $query;
        }

		$sql = <<<SQL
			UPDATE
				$this->_name SET mixtura = YEAR([fecha cierre]) & Id 
SQL;
		return $this->_conn->query($sql);
	}

	public function editMixtura($mixtura) {
        // Denegar accion por edicion
        if (FALSE == EDITABLE) {
            return FALSE;
        }
    
		$sql = <<<SQL
	UPDATE $this->_name 
	SET 
        [fecha cierre]=?,
        [Forecast]=?,
        [% Sulfuros]=?,
        [PESO HUMEDO]=?,
        [% Oxidos (minerales)]=?,
        [% Arena]=?,
        [% Piedra de Cal]=?,
        [% Secs Refineria]=?,
        [% Secs Bermejillo]=?,
        [% Secs de Zinc]=?,
        [% Secs Fund]=?,
        [% Casc Fe Tons]=?,
        [% Ins]=?,
        [% SiO2 CALC]=?, 
        [% Fe CALC]=?,
        [% CaO CALC]=?,
        [% Zn CALC]=?,
        [% S total CALC]=?,
        [% S util CALC]=?,
        [% Pb CALC]=?,
        [% Cu CALC]=?,
        [% As CALC]=?,
        [% Sb CALC]=?,
        [% Cd CALC]=?,
        [gr/ton Au CALC]=?,
        [kg/ton Ag CALC]=?,
        [Ag/Pb CALC]=?,
        [suma total ensayes CAL]=?, 
        [Fe/SiO2 mixtura CALC]=?,
        [Fe/SiO2 mixtura + cascarilla CALC]=?,
        [CaO/SiO2 CALC]=?,
        [Sb/Pb CALC]=?,
        [Cu/Pb CALC]=?,
        [Cu/As CALC]=?,
        [As/Sb CALC]=?,
        [Suma de ensayes funde]=?,
        [Suma de ensayes no funde]=?,
        [TOTAL SECS CALC]=?,
        [Tonelaje Costo CALC]=?, 
        [Suma de Fundentes]=?, 
        [Cu/(As+Sb) Calc]=?,
        [Fecha_Captura]=?,
        [% S/SO4 CALC]=?
	WHERE 
        [Id]                                    = ?
SQL;

		return $this->_conn->prepare($sql)
            ->execute(array(
                $mixtura['fecha cierre'],
                $mixtura['Forecast'],
                $mixtura['% Sulfuros'],
                $mixtura['PESO HUMEDO'],
                $mixtura['% Oxidos (minerales)'],
                $mixtura['% Arena'],
                $mixtura['% Piedra de Cal'],
                $mixtura['% Secs Refineria'],
                $mixtura['% Secs Bermejillo'],
                $mixtura['% Secs de Zinc'],
                $mixtura['% Secs Fund'],
                $mixtura['% Casc Fe Tons'],
                $mixtura['% Ins'],
                $mixtura['% SiO2 CALC'],
                $mixtura['% Fe CALC'],
                $mixtura['% CaO CALC'],
                $mixtura['% Zn CALC'],
                $mixtura['% S total CALC'],
                $mixtura['% S util CALC'],
                $mixtura['% Pb CALC'],
                $mixtura['% Cu CALC'],
                $mixtura['% As CALC'],
                $mixtura['% Sb CALC'],
                $mixtura['% Cd CALC'],
                $mixtura['gr/ton Au CALC'],
                $mixtura['kg/ton Ag CALC'],
                $mixtura['Ag/Pb CALC'],
                $mixtura['suma total ensayes CAL'],
                $mixtura['Fe/SiO2 mixtura CALC'],
                $mixtura['Fe/SiO2 mixtura + cascarilla CALC'],
                $mixtura['CaO/SiO2 CALC'],
                $mixtura['Sb/Pb CALC'],
                $mixtura['Cu/Pb CALC'],
                $mixtura['Cu/As CALC'],
                $mixtura['As/Sb CALC'],
                $mixtura['Suma de ensayes funde'],
                $mixtura['Suma de ensayes no funde'],
                $mixtura['TOTAL SECS CALC'],
                $mixtura['Tonelaje Costo CALC'],
                $mixtura['Suma de Fundentes'],
                $mixtura['Cu/(As+Sb) Calc'],
                $mixtura['Fecha_Captura'],
                $mixtura['% S/SO4 CALC'],
                $mixtura['id']
            )); 
	}

	public function getMixtura($id) {
		$sql = <<<SQL
		SELECT * FROM $this->_name WHERE id = ?
SQL;

        $stmt = $this->_conn->prepare($sql);
        $stmt->execute(array($id));

        if (FALSE === $stmt) {
            return $stmt;
        }

        return $stmt->fetch(PDO::FETCH_OBJ);
	}

	public function __destruct () {
		parent::__destruct();
	}
}
